import React, { Component }  from 'react';
// import { Link } from 'react-router-dom';

import LoginService from '../services/Login';

import { NAV } from 'config/config';

import HeaderCon from '../components/IndexCons/HeaderCon';
import SideBarCon from '../components/IndexCons/SideBarCon';
import ContainerCon from 'components/IndexCons/ContainerCon';

const loginService = new LoginService();

class IndexPage extends Component {
  constructor (props) {
    super(props);
    this.state = {
      curIdx: 0,
      title: NAV[0].title,
      field: NAV[0].field
    }
  }
  //相当于原型上的state赋值

  componentDidMount () {
    this.loginCheck();
  }

 

  /* 检查登录状态*/
  async loginCheck () {
    const result = await loginService.loginCheck();
    const errorCode = result.error_code;
    const { history } = this.props;

    if(errorCode === 10006) {
      //判断是不是非登录状态
      history.push('/login ')
      window.location.reload();
      return;
    }

    history.push('/course')
  }

  /* 切换导航栏*/
  async onNavItemClick (dataItem, index) {
    const { field, title } = dataItem;
    this.setState({
      field,
      title,
      curIdx: index
    })
  }

  render () {
    const { children,  history } = this.props;
    const { curIdx } = this.state;

    return (
      <div className="container">
          <HeaderCon history={ history} />
          <SideBarCon 
            curIdx = { curIdx }
            onNavItemClick = { this.onNavItemClick.bind(this) }
          />
          <ContainerCon 
            children = { children} />
      </div>
    )
  }
}

export default IndexPage;